<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form inline :model="form" ref="form" class="demo-form-inline">
        <el-form-item prop="courseName" label="课程名称">
          <el-input v-model="form.courseName" placeholder="课程名称"></el-input>
        </el-form-item>
        <el-form-item prop="status" label="状态">
          <el-select v-model="form.status" placeholder="状态">
            <el-option
              v-for="item in options"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button :disabled="isLoading" type="primary" @click="onSubmit"
            >查询</el-button
          >
          <el-button :disabled="isLoading" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button @click="$router.push({ name: 'course-new'})">添加课程</el-button>
    </div>

    <el-table :data="courses" style="width: 100%" v-loading="isLoading">
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="courseName" label="课程名称"></el-table-column>
      <el-table-column prop="price" label="价格">
        <template slot-scope="scope">
          <span v-if="scope.row.price">¥{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sortNum" label="排序"> </el-table-column>
      <el-table-column prop="status" align="center" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            :disabled="scope.row.isStatusLoading"
            @change="handleCourseState(scope.row)">
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" :width="260" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleSelectUser(scope.row)">
            编辑
          </el-button>
          <el-button size="mini" @click="handleSelectUser(scope.row)">
            内容管理
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :disabled="isLoading"
      :current-page.sync="form.current"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="form.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
import { getQueryCourses, changeState } from '@/services/course'
import { Form } from 'element-ui'
export default Vue.extend({
  name: 'CourseList',
  data () {
    return {
      options: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 0,
          label: '下架'
        },
        {
          value: 1,
          label: '上架'
        }
      ],
      form: {
        currentPage: 1,
        pageSize: 10,
        courseName: '',
        status: ''
      },
      courses: [],
      isLoading: false,
      total: 0
    }
  },
  created () {
    this.loadCourses()
  },
  methods: {
    async loadCourses () {
      this.isLoading = true
      const { data } = await getQueryCourses(this.form)
      data.data.records.forEach((item: any) => { item.isStatusLoading = false })
      this.courses = data.data.records
      this.total = data.data.total
      this.isLoading = false
    },
    handleSizeChange (val: number) {
      this.form.currentPage = 1
      this.form.pageSize = val
      this.loadCourses()
    },
    onSubmit () {
      console.log('onSubmit')
      this.loadCourses()
    },
    onReset () {
      (this.$refs.form as Form).resetFields()
      this.loadCourses()
    },
    handleCurrentChange (val: number): void {
      this.form.currentPage = val
      this.loadCourses()
    },

    async handleCourseState (course: any) {
      course.isStatusLoading = true
      await changeState({ courseId: course.id, status: course.status })
      this.$message.success(course.status === 0 ? '下架成功' : '上架成功')
      course.isStatusLoading = false
    }
  }
})
</script>

<style lang="scss" scoped>
</style>
